package com.dmz.task.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmz.task.bean.dto.DocInfoAddDto;
import com.dmz.task.bean.dto.DocInfoEditDto;
import com.dmz.task.bean.dto.DocInfoQueryDto;
import com.dmz.task.bean.entity.TDocInfo;
import com.dmz.task.constant.GlobalConstant;
import com.dmz.task.mapper.DocInfoMapper;
import com.dmz.task.model.PageModel;
import com.dmz.task.utils.AuthUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.stream.Collectors;

@Service
public class DocInfoService {

    @Autowired
    private DocInfoMapper docInfoMapper;

    public PageModel<TDocInfo> query(DocInfoQueryDto queryParam) {
        PageModel<TDocInfo> responsePage = new PageModel<>(queryParam.getPageNo(), queryParam.getPageSize());

        LambdaQueryWrapper<TDocInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TDocInfo::getDelFlag, GlobalConstant.NOT_DELETED);
        queryWrapper.like(StringUtils.hasLength(queryParam.getDocName()),
                TDocInfo::getDocName, queryParam.getDocName());

        Page<TDocInfo> logPage = docInfoMapper.selectPage(Page.of(queryParam.getPageNo(), queryParam.getPageSize()), queryWrapper);

        if (logPage != null && !CollectionUtils.isEmpty(logPage.getRecords())) {
            responsePage.setTotalPage(logPage.getPages());
            responsePage.setTotalCount(logPage.getTotal());
            responsePage.setRecords(logPage.getRecords().stream().map(x -> {
                TDocInfo vo = new TDocInfo();
                BeanUtils.copyProperties(x, vo);
                return vo;
            }).collect(Collectors.toList()));
        }

        return responsePage;
    }

    public TDocInfo select(Long id) {
        return docInfoMapper.selectById(id);
    }

    public Boolean add(DocInfoAddDto dto) {
        TDocInfo docInfo = new TDocInfo();
        docInfo.setDocName(dto.getDocName());
        docInfo.setSrcHost(dto.getSrcHost());
        docInfo.setSrcPort(dto.getSrcPort());
        docInfo.setSrcPath(dto.getSrcPath());
        docInfo.setSrcLoc(dto.getSrcLoc());
        docInfo.setFileName(dto.getFileName());
        docInfo.setSrcUsername(dto.getSrcUsername());
        docInfo.setSrcPassword(dto.getSrcPassword());
        docInfo.setTgtHost(dto.getTgtHost());
        docInfo.setTgtPort(dto.getTgtPort());
        docInfo.setTgtPath(dto.getTgtPath());
        docInfo.setTgtLoc(dto.getTgtLoc());
        docInfo.setTgtUsername(dto.getTgtUsername());
        docInfo.setTgtPassword(dto.getTgtPassword());
        docInfo.setRemark(dto.getRemark());
        docInfo.setDelFlag(GlobalConstant.NOT_DELETED);
        docInfo.setCreatedBy((String) AuthUtils.getLoginId());
        int rows = this.docInfoMapper.insert(docInfo);
        return rows > 0;
    }


    public Boolean edit(DocInfoEditDto dto) {
        LambdaUpdateWrapper<TDocInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TDocInfo::getId, dto.getId());
        wrapper.set(TDocInfo::getDocName, dto.getDocName());
        wrapper.set(TDocInfo::getSrcHost, dto.getSrcHost());
        wrapper.set(TDocInfo::getSrcPort, dto.getSrcPort());
        wrapper.set(TDocInfo::getSrcPath, dto.getSrcPath());
        wrapper.set(TDocInfo::getSrcLoc, dto.getSrcLoc());
        wrapper.set(TDocInfo::getFileName, dto.getFileName());
        wrapper.set(TDocInfo::getSrcUsername, dto.getSrcUsername());
        wrapper.set(TDocInfo::getSrcPassword, dto.getSrcPassword());
        wrapper.set(TDocInfo::getTgtHost, dto.getTgtHost());
        wrapper.set(TDocInfo::getTgtPort, dto.getTgtPort());
        wrapper.set(TDocInfo::getTgtPath, dto.getTgtPath());
        wrapper.set(TDocInfo::getTgtLoc, dto.getTgtLoc());
        wrapper.set(TDocInfo::getTgtUsername, dto.getTgtUsername());
        wrapper.set(TDocInfo::getTgtPassword, dto.getTgtPassword());
        wrapper.set(TDocInfo::getRemark, dto.getRemark());
        wrapper.set(TDocInfo::getUpdatedBy, (String) AuthUtils.getLoginId());
        int rows = this.docInfoMapper.update(null, wrapper);
        return rows > 0;
    }

    public Boolean delete(Long id) {
        // 逻辑删除
        LambdaUpdateWrapper<TDocInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TDocInfo::getId, id);
        wrapper.set(TDocInfo::getDelFlag, GlobalConstant.DELETED);
        wrapper.set(TDocInfo::getUpdatedBy, (String) AuthUtils.getLoginId());
        int rows = this.docInfoMapper.update(null, wrapper);
        return rows > 0;
    }
}
